package com.yxuan.springboot.controller;

import com.yxuan.springboot.model.Operation;
import com.yxuan.springboot.service.OperationService;
import com.yxuan.springboot.util.CodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Map;


/**
 * 
 * @author zchao
 *   Operation控制器
 * @date 2023-05-14 20:40:39
 */
@Slf4j
@Controller
@Api(tags = "文章操作表")
@RequestMapping(value = "/operation", method = {RequestMethod.POST,RequestMethod.GET})
public class OperationController extends BaseController<Operation>{

    @Autowired
    public OperationService operationService;

    /**
     * @className: OperationController.java
     * @methodName: deleteOperation
     * @effect: 删除文章的操作（0点赞，1收藏，2阅读，3分享）
     * @author: JingxuanFan
     * @date: 2023/5/28 21:15
     * @param operation 文章操作对象，对象中需要传递三个值：userLogin 用户Id，information 文章Id，operation 文章操作（0点赞，1收藏，2阅读，3分享）
    **/
    @ApiOperation(value = "删除文章操作", notes = "删除文章的操作（0点赞，1收藏，2阅读，3分享）",httpMethod = "GET")
    @GetMapping("/deleteOperation")
    @ResponseBody
    public Map<String, Object> deleteOperation(@RequestBody Operation operation){
        try {
            log.info("");
            //查询文章操作数据
            Operation queryOperation = operationService.findObjectByPramas(operation);
            //修改状态
            queryOperation.setDeleteState("1");
            operationService.upd(queryOperation);
            return ResponseMap(CodeUtil.Susser,null,"操作成功");
        }catch (Exception e){
            log.error(e.getMessage());
            return ResponseMap(CodeUtil.Error,e.getMessage(),"删除失败");
        }
    }


    /**
     * @className: OperationController.java
     * @methodName: addOperation
     * @effect: 用户对文章进行操作
     * @author: JingxuanFan
     * @date: 2023/5/29 18:57
     * @param operation 文章操作对象
    **/
    @ApiOperation(value = "添加文章操作", notes = "添加用户对文章的操作（0点赞，1收藏，2阅读，3分享）",httpMethod = "GET")
    @GetMapping("/addOperation")
    @ResponseBody
    public Map<String,Object> addOperation(Operation operation){
        try{
            if (operationService.addOperation(operation) > 0){
                return ResponseMap(CodeUtil.Susser,null,"添加成功");
            }
           return ResponseMap(CodeUtil.Error,"操作失败",null);
        }catch (Exception e){
            log.error(e.getMessage());
            return ResponseMap(CodeUtil.Error,e.getMessage(),null);
        }
    }


}
